package util.pagingOpt.impl;

import dao.admins.impl.GoodsQueryImpl;
import pojo.Goods;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodsImpl extends Paging {
    public GoodsImpl(int pageSize, String sqlCount, String sqlQuery) {
        super(pageSize, sqlCount, sqlQuery);
    }

    @Override
    public List<Goods> getList() {
        return queryList();
    }

    private  List<Goods> queryList(){
        List<Goods> goods = new ArrayList<>();
        Object[] params = {(pageEntity.getCurrentPage() - 1) * pageEntity.getPageSize(), pageEntity.getPageSize()};
        try(ResultSet rs = baseDao.executeSelect(sqlPaging, params)){
            while (rs.next()){
                Goods good = new Goods();
                GoodsQueryImpl.setGoods(good, rs.getLong("id"), rs.getString("goodsName"), rs.getString("smallName"), rs.getDouble("goodsMoney"), rs.getInt("goodsNumber"),
                        rs.getString("goodsImage"), rs.getDouble("goodsCarriage"), rs.getInt("goodsType"), rs.getString("sellerName"), rs.getDouble("discRate"));
//                System.out.println(good.toString());
                goods.add(good);
            }
            baseDao.closeAll();
//            System.out.println(goods);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return goods;
    }
}
